/*
 * Copyright (c) 2009-2012 jMonkeyEngine
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
 *   may be used to endorse or promote products derived from this software
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.mechanatrix.input;

import com.jme3.app.SimpleApplication;
import com.jme3.input.ChaseCamera;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.AnalogListener;
import com.jme3.input.controls.JoyAxisTrigger;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.material.Material;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Quad;
import com.mechanatrix.input.ThirdPersonCamera;
import com.uhfsat.UHFControl;

/**
 * A 3rd-person chase camera orbits a target (teapot). Follow the teapot with
 * WASD keys, rotate by dragging the mouse.
 */
public class Test3rdPerson extends SimpleApplication implements AnalogListener, ActionListener {

	private Spatial model;
	private ThirdPersonCamera cameraControl;

	public static void main(String[] args) {
		Test3rdPerson app = new Test3rdPerson();
		app.start();
	}

	@Override
	public void simpleInitApp() {
		// Load a teapot model
		model = (Spatial) assetManager.loadModel("Models/UHFSatcom.obj");
		Material mat_tea = new Material(assetManager, "Common/MatDefs/Misc/ShowNormals.j3md");
		model.setMaterial(mat_tea);
		rootNode.attachChild(model);

		// Load a floor model
		Material mat_ground = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
		mat_ground.setTexture("ColorMap", assetManager.loadTexture("Textures/metal_plates_02_dif.tga"));
		Geometry ground = new Geometry("ground", new Quad(50, 50));
		ground.setLocalRotation(new Quaternion().fromAngleAxis(-FastMath.HALF_PI, Vector3f.UNIT_X));
		ground.setLocalTranslation(-25, -10, 25);
		ground.setMaterial(mat_ground);
		rootNode.attachChild(ground);

		// Disable the default first-person cam!
		flyCam.setEnabled(false);

		// Enable a chase cam
		cameraControl = new ThirdPersonCamera(cam, model, inputManager);

		//Uncomment this to invert the camera's vertical rotation Axis
		//chaseCam.setInvertVerticalAxis(true);

		//Uncomment this to invert the camera's horizontal rotation Axis
		//chaseCam.setInvertHorizontalAxis(true);

		//Comment this to disable smooth camera motion
		cameraControl.setSmoothMotion(true);

		//Uncomment this to disable trailing of the camera
		//WARNING, trailing only works with smooth motion enabled. It is true by default.
		//chaseCam.setTrailingEnabled(false);

		//Uncomment this to look 3 world units above the target
		//chaseCam.setLookAtOffset(Vector3f.UNIT_Y.mult(3));

		//Uncomment this to enable rotation when the middle mouse button is pressed (like Blender)
		//WARNING : setting this trigger disable the rotation on right and left mouse button click
		//chaseCam.setToggleRotationTrigger(new MouseButtonTrigger(MouseInput.BUTTON_MIDDLE));

		//Uncomment this to set mutiple triggers to enable rotation of the cam
		//Here spade bar and middle mouse button
		//chaseCam.setToggleRotationTrigger(new MouseButtonTrigger(MouseInput.BUTTON_MIDDLE),new KeyTrigger(KeyInput.KEY_SPACE));

		//registering inputs for target's movement
		registerInput();

	}

	public void registerInput() {
		inputManager.addMapping(UHFControl.MOVE_UP,
				new KeyTrigger(KeyInput.KEY_UP),
				new JoyAxisTrigger(0, 2, true)); //up on moveLeft stick
		inputManager.addMapping(UHFControl.MOVE_DOWN,
				new KeyTrigger(KeyInput.KEY_DOWN),
				new JoyAxisTrigger(0, 2, false)); //down on moveLeft stick
		inputManager.addMapping(UHFControl.MOVE_LEFT,
				new KeyTrigger(KeyInput.KEY_LEFT),
				new JoyAxisTrigger(0, 3, true)); //left on moveLeft stick
		inputManager.addMapping(UHFControl.MOVE_RIGHT,
				new KeyTrigger(KeyInput.KEY_RIGHT),
				new JoyAxisTrigger(0, 3, false)); //right on moveLeft stick
		inputManager.addMapping(UHFControl.START,
				new KeyTrigger(KeyInput.KEY_RETURN));
		/*
		inputManager.addMapping(UHFControl.LOOK_UP,
				new JoyAxisTrigger(0, 0, true)); //up on right stick
		inputManager.addMapping(UHFControl.LOOK_DOWN,
				new JoyAxisTrigger(0, 0, false)); //down on right stick
		inputManager.addMapping(UHFControl.LOOK_LEFT,
				new JoyAxisTrigger(0, 1, true)); //left on right stick
		inputManager.addMapping(UHFControl.LOOK_RIGHT,
				new JoyAxisTrigger(0, 1, false)); //right on right stick
		*/
		inputManager.addListener(this,
				UHFControl.MOVE_UP,
				UHFControl.MOVE_DOWN,
				UHFControl.MOVE_RIGHT,
				UHFControl.MOVE_LEFT,
				UHFControl.START/*,
				UHFControl.LOOK_UP,
				UHFControl.LOOK_DOWN,
				UHFControl.LOOK_LEFT,
				UHFControl.LOOK_RIGHT*/);
	}

	@Override
	public void onAnalog(String name, float value, float tpf) {
		switch (name) {
			case UHFControl.MOVE_LEFT:
				model.move(-5 * tpf, 0, 0);
				break;
			case UHFControl.MOVE_RIGHT:
				model.move(5 * tpf, 0, 0);
				break;
			case UHFControl.MOVE_UP:
				model.move(0, 0, -5 * tpf);
				break;
			case UHFControl.MOVE_DOWN:
				model.move(0, 0, 5 * tpf);
				break;
			/*
			case UHFControl.LOOK_UP:
				cameraControl.verticalRotate(value);
				break;
			case UHFControl.LOOK_DOWN:
				cameraControl.verticalRotate(-value);
				break;
			case UHFControl.LOOK_LEFT:
				cameraControl.horizontalRotate(-value);
				break;
			case UHFControl.LOOK_RIGHT:
				cameraControl.horizontalRotate(value);
				break;
			*/
		}

	}

	@Override
	public void onAction(String name, boolean keyPressed, float tpf) {
		if (name.equals(UHFControl.START) && keyPressed) {
			model.move(10, 10, 10);
		}
	}

	@Override
	public void simpleUpdate(float tpf) {
		super.simpleUpdate(tpf);

		//  model.move(new Vector3f(0.001f, 0, 0));
		// pivot.rotate(0, 0.00001f, 0);
		//   rootNode.updateGeometricState();
	}

	@Override
	public void update() {
		super.update();
		// render the viewports
		// float tpf = timer.getTimePerFrame();
		// state.getRootNode().rotate(0, 0.000001f, 0);
		// stateManager.update(tpf);
		// stateManager.render(renderManager);
		// renderManager.render(tpf);
	}
}
